package com.goods.controller.business;

import com.goods.business.service.IProductCategoryService;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 *
 * @Date: 2023/3/6 - 10:22
 * @Author: Hyoung
 * @Description:
 */
@Api(tags = "物资类别模块-物资类别")
@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {
    //http://www.localhost:8989/business/productCategory/categoryTree?pageNum=1&pageSize=5
    @Autowired
    private IProductCategoryService iProductCategoryService;

    /**
     * 分页树型显示
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/categoryTree")
    public ResponseBean page(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        PageVO<ProductCategoryTreeNodeVO> departmentsList =
                iProductCategoryService.findAll(pageNum, pageSize);

        return ResponseBean.success(departmentsList);
    }

    /**
     * 添加时用作分类列表
     *
     * @return
     */
    @GetMapping("/getParentCategoryTree")
    public ResponseBean getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> departmentsList = iProductCategoryService.getParentCategoryTree();
        return ResponseBean.success(departmentsList);
    }

    /**
     * 添加一个分类
     * addRuleForm: { pid: "0", name: "", remark: "", sort: "" }, //添加表单
     *
     * @return
     */
    @PostMapping("/add")
    public ResponseBean addProductCategory(@RequestBody @Validated ProductCategoryVO productCategoryVO) {
        iProductCategoryService.addProductCategory(productCategoryVO);
        return ResponseBean.success();
    }

    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Integer id) {
        iProductCategoryService.delete(id);
        return ResponseBean.success();
    }

    @GetMapping("/edit/{id}")
    public ResponseBean findProductCategoryById(@PathVariable Integer id) {
        ProductCategoryVO productCategoryVO = iProductCategoryService.findProductCategoryById(id);
        return ResponseBean.success(productCategoryVO);
    }

    @PutMapping("/update/{id}")
    public ResponseBean updateProductCategoryById(@PathVariable Integer id, @RequestBody @Validated ProductCategoryVO productCategoryVO) {
        iProductCategoryService.updateProductCategoryById(id, productCategoryVO);

        return ResponseBean.success();
    }
}
